//
//  loginPassWordViewController.m
//  HJB_project
//
//  Created by jiezsoft.com on 14-11-12.
//  Copyright (c) 2014年 jiezsoft.com. All rights reserved.
//

#import "editAddressViewController.h"
#import "TheLoginViewController.h"
#import "UIViewExt.h"
#import "HAODEsingleton.h"
#import "MyMD5.h"
#import "HZAreaPickerView.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "UIImage+RTTint.h"
@interface editAddressViewController ()<UITextFieldDelegate,UIGestureRecognizerDelegate ,UINavigationControllerDelegate,HZAreaPickerDelegate>
@property (strong, nonatomic) NSString *areaValue;
@property (strong, nonatomic) NSString*cityValue;
@property (strong, nonatomic) HZAreaPickerView *locatePicker;
@property(nonatomic,retain)UIDatePicker* da;
@property (nonatomic,retain)NSString*jzdprovince;
@property (nonatomic,retain)NSString*jzdcity;
@property (nonatomic,retain)NSString*jzddistrict;
-(void)cancelLocatePicker;
@end

@implementation editAddressViewController

{
    UITextField* UserNameTextField;
    UITextField* PasswordTextField;
    UITextField* PasswordTextField1;
    UITextField* new;
    UIView*view1;
    UIView* myToolBar;
    UIButton*agreementBtn;
    UIButton*agreementBtn1;
    BOOL saveAccount;
    BOOL saveAccount1;
    UIButton*login;
    HAODEsingleton*commdity;
    UITextField*diquTextField1;
    UIButton* chooseBtn;
    NSString*isDefault;
    
}
@synthesize alt = alt;
@synthesize areaValue=_areaValue, cityValue=_cityValue;
@synthesize locatePicker=_locatePicker;

-(void)setAreaValue:(NSString *)areaValue
{
    if (![_areaValue isEqualToString:areaValue]) {
        
        
        
        diquTextField1.text = areaValue;
        
        
    }
}

-(void)setCityValue:(NSString *)cityValue
{
    if (![_cityValue isEqualToString:cityValue]) {
        //        _cityValue = [cityValue retain];
        diquTextField1.text = cityValue;
    }
}

#pragma mark - HZAreaPicker delegate
-(void)pickerDidChaneStatus:(HZAreaPickerView *)picker
{
    if (picker.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        self.areaValue = [NSString stringWithFormat:@"%@ %@ %@", picker.locate.state, picker.locate.city, picker.locate.district];
        
        
        
        self.jzdprovince= picker.locate.state;
        self.jzdcity=picker.locate.city;
        self.jzddistrict=picker.locate.district;
        
        
        
    } else{
        self.cityValue = [NSString stringWithFormat:@"%@ %@", picker.locate.state, picker.locate.city];
        
    }
}
-(void)area:(UIButton*)sender{
    
    
    [UserNameTextField resignFirstResponder];
    [PasswordTextField resignFirstResponder];
    [PasswordTextField1 resignFirstResponder];
    [diquTextField1 resignFirstResponder];
    
    
    
    [self cancelLocatePicker];
    self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithStateAndCityAndDistrict delegate:self] ;
    [self.locatePicker showInView:self.view];
    
    diquTextField1.text=@"北京 通州";
    
    self.jzdprovince=@"北京";
    self.jzdcity=@"通州";
    
    
    
}

-(void)cancelLocatePicker
{
    [self.locatePicker cancelPicker];
    self.locatePicker.delegate = nil;
    self.locatePicker = nil;
}


#pragma mark - TextField delegate

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self cancelLocatePicker];
}

-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
       UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改收货地址";
    commdity=[HAODEsingleton shared];
    // 手势
    isDefault=@"N";
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [self.view addGestureRecognizer:singleFingerOne];
    
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    
    self.view.backgroundColor=[UIColor whiteColor];
    UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(20,170*ScreenHeight/1334+64, ScreenWidth-40, 0.35)];
    line1.backgroundColor = labelColor;
    [self.view addSubview:line1];
    UILabel *line3 = [[UILabel alloc]initWithFrame:CGRectMake(20,255*ScreenHeight/1334+64, ScreenWidth-40, 0.35)];
    line3.backgroundColor = labelColor;
    [self.view addSubview:line3];
    UILabel *line13 = [[UILabel alloc]initWithFrame:CGRectMake(20,345*ScreenHeight/1334+64, ScreenWidth-40, 0.35)];
    line13.backgroundColor = labelColor;
    [self.view addSubview:line13];
    UILabel *line134 = [[UILabel alloc]initWithFrame:CGRectMake(20,435*ScreenHeight/1334+64, ScreenWidth-40, 0.35)];
    line134.backgroundColor = labelColor;
    [self.view addSubview:line134];
    
    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(20.0f,90*ScreenHeight/1334+64, 80.0f,80*ScreenHeight/1334)];
    label.text=@"收货人";
    [label setTextColor:[UIColor colorWithRed:153.00/255.00 green:153/255.00 blue:153/255.00 alpha:1.0]];
    label.font = [UIFont systemFontOfSize:contenfot];
    [self.view addSubview:label];
    UserNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(label.right+3,90*ScreenHeight/1334+64, 180.0f,80*ScreenHeight/1334)];
    [UserNameTextField setBorderStyle:UITextBorderStyleNone]; //外框类型
    UserNameTextField.keyboardType = UIKeyboardTypeDefault;
    UserNameTextField.placeholder = @"请输入收货人姓名"; //默认显示的字
    UserNameTextField.font=[UIFont systemFontOfSize:contenfot];
    UserNameTextField.returnKeyType = UIReturnKeyDone;
    UserNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    UserNameTextField.delegate = self;
    UserNameTextField.tag=22;
    UserNameTextField.text=commdity.title;
    [self.view addSubview:UserNameTextField];
    
    
    myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:daxiao];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];
    UserNameTextField.inputAccessoryView = myToolBar;
    
    
    UILabel* label1=[[UILabel alloc]initWithFrame:CGRectMake(20.0f,170*ScreenHeight/1334+64, 80.0f,80*ScreenHeight/1334)];
    label1.text=@"手机号";
    [label1 setTextColor:[UIColor colorWithRed:153.00/255.00 green:153/255.00 blue:153/255.00 alpha:1.0]];
    label1.font = [UIFont systemFontOfSize:contenfot];
    [self.view addSubview:label1];
    PasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(label1.right+3,170*ScreenHeight/1334+64, 180.0f,80*ScreenHeight/1334)];
    [PasswordTextField setBorderStyle:UITextBorderStyleNone]; //外框类型
    PasswordTextField.placeholder = @"请输入会员的手机号码"; //默认显示的字
    PasswordTextField.keyboardType = UIKeyboardTypeDecimalPad;
    PasswordTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    PasswordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    PasswordTextField.returnKeyType = UIReturnKeyDone;
    PasswordTextField.font=[UIFont systemFontOfSize:contenfot];
    PasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    PasswordTextField.delegate = self;
    PasswordTextField.text=commdity.Phone;
    [PasswordTextField addTarget:self action:@selector(userNumberLimitTextLength:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:PasswordTextField];
    PasswordTextField.inputAccessoryView = myToolBar;
    
    
    
    UILabel* diqu=[[UILabel alloc]initWithFrame:CGRectMake(20.0f,260*ScreenHeight/1334+64, 80.0f,80*ScreenHeight/1334)];
    diqu.text=@"地区";
    [diqu setTextColor:[UIColor colorWithRed:153.00/255.00 green:153/255.00 blue:153/255.00 alpha:1.0]];
    diqu.font = [UIFont systemFontOfSize:contenfot];
    [self.view addSubview:diqu];
    UIButton*area=[[UIButton alloc]initWithFrame:CGRectMake(diqu.right-15, 260*ScreenHeight/1334+64, ScreenWidth, 88/2)];
    [area addTarget:self action:@selector(area:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:area];
    UIButton*jiantouBtn3 = [[UIButton alloc]init];
    jiantouBtn3.frame = CGRectMake(ScreenWidth-30,290*ScreenHeight/1334+64,8, 13);
    [jiantouBtn3 setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
//    [self.view addSubview:jiantouBtn3];
    
    diquTextField1 = [[UITextField alloc] initWithFrame:CGRectMake(diqu.right+3,260*ScreenHeight/1334+64, 180.0f,80*ScreenHeight/1334)];
    [diquTextField1 setBorderStyle:UITextBorderStyleNone]; //外框类型
    diquTextField1.placeholder = @"请选择现在所在地区"; //默认显示的字
    diquTextField1.autocorrectionType = UITextAutocorrectionTypeNo;
    diquTextField1.autocapitalizationType = UITextAutocapitalizationTypeNone;
    diquTextField1.returnKeyType = UIReturnKeyDone;
    diquTextField1.font=[UIFont systemFontOfSize:contenfot];
    diquTextField1.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    diquTextField1.delegate = self;
    
    
    if (![commdity. ProvinceName isEqual:[NSNull null]]) {
        self.jzdprovince= commdity. ProvinceName;
        self.jzdcity=commdity. CityName;
        self.jzddistrict=commdity. RegionName;
        
        
        diquTextField1.text=[NSString stringWithFormat:@"%@,%@,%@",  commdity. ProvinceName,commdity. CityName,commdity. RegionName];
        
    }


    [diquTextField1 setUserInteractionEnabled:NO];
    [self.view addSubview:diquTextField1];
    
    
    UILabel* label11=[[UILabel alloc]initWithFrame:CGRectMake(20.0f,350*ScreenHeight/1334+64, 80.0f,80*ScreenHeight/1334)];
    label11.text=@"详细地址";
    [label11 setTextColor:[UIColor colorWithRed:153.00/255.00 green:153/255.00 blue:153/255.00 alpha:1.0]];
    label11.font = [UIFont systemFontOfSize:contenfot];
    [self.view addSubview:label11];
    PasswordTextField1 = [[UITextField alloc] initWithFrame:CGRectMake(label11.right+3,350*ScreenHeight/1334+64, 500*ScreenWidth/750,80*ScreenHeight/1334)];
    [PasswordTextField1 setBorderStyle:UITextBorderStyleNone]; //外框类型
    PasswordTextField1.placeholder = @"请输入详细地址"; //默认显示的字
    PasswordTextField1.keyboardType = UIKeyboardTypeDefault;
    PasswordTextField1.autocorrectionType = UITextAutocorrectionTypeNo;
    PasswordTextField1.autocapitalizationType = UITextAutocapitalizationTypeNone;
    PasswordTextField1.returnKeyType = UIReturnKeyDone;
    PasswordTextField1.font=[UIFont systemFontOfSize:contenfot];
    PasswordTextField1.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    PasswordTextField1.delegate = self;
    PasswordTextField1.text=commdity.adress;
    [self.view addSubview:PasswordTextField1];
    
    
    PasswordTextField1.inputAccessoryView = myToolBar;
    
    
    chooseBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, PasswordTextField1.bottom+20, 30, 30)];
    
    NSLog(@"------%@",commdity.isDefault);
    if ([commdity.isDefault isEqualToString:@"1"]) {
          isDefault=@"Y";
         [chooseBtn setImage:[UIImage imageNamed:@"jizhumima_hight"] forState:UIControlStateNormal];
        
    }else{
          isDefault=@"N";
        [chooseBtn setImage:[UIImage imageNamed:@"sheweimoren_normal"] forState:UIControlStateNormal];
        
    }
    [chooseBtn addTarget:self action:@selector(chooseBtn1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:chooseBtn];
    
    UIButton*jiadaagreementBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, PasswordTextField1.bottom+20, 44, 44)];
    [jiadaagreementBtn addTarget:self action:@selector(chooseBtn1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:jiadaagreementBtn];
    
    
    UILabel* Rememberthepassword=[[UILabel alloc]initWithFrame:CGRectMake(chooseBtn.right+2, PasswordTextField1.bottom+27, 120.0f, 16.0)];
    Rememberthepassword.text=@"  设置默认";
    Rememberthepassword.font = [UIFont systemFontOfSize: 15];
    [Rememberthepassword setTextColor:[UIColor grayColor]];
    [self.view addSubview:Rememberthepassword];
    
    
    [login removeFromSuperview];
    login = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenHeight-45, ScreenWidth, 45)];
    [login setTitle:@"确认" forState:UIControlStateNormal];
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    login.backgroundColor=luseColor;
    //    [login.layer setCornerRadius:3.0]; //设置矩圆角半径
    //    [login.layer setBorderWidth:0.3];   //边框宽度
    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){204.0/255.0,153.0/255.0, 51.0/255.0, 1.0});
    [login.layer setBorderColor:tureBTncolorref];//边框颜色
    [login addTarget:self action:@selector(ture:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:login];
    
    
    
    
    
    // Do any additional setup after loading the view from its nib.
}
//号码限制
- (void)userNumberLimitTextLength:(UITextField*)textField
{
    
    
    if ([textField.text length] >= 11)
    {
        textField.text = [textField.text substringToIndex:11];
        
        
        
        
    }
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self cancelLocatePicker];
    
}
-(void)chooseBtn1:(id)sender
{
    saveAccount=!saveAccount;
    if (saveAccount) {
        isDefault=@"Y";
          [chooseBtn setImage:[UIImage imageNamed:@"jizhumima_hight"] forState:UIControlStateNormal];        
    }
    else
    {isDefault=@"N";
        [chooseBtn setImage:[UIImage imageNamed:@"sheweimoren_normal"] forState:UIControlStateNormal];
     
    }
    
}


-(void)ture:(UIButton*)sender{
    
    if ([UserNameTextField.text length] < 1) {
        [self showAlertMessage:@"请输入姓名"];
    } else{
        if ([PasswordTextField.text length] < 1)
        {
            [self showAlertMessage:@"请输入会员手机号码"];
        }
        else
        {
            if ([self.jzdprovince length] < 1)
            {
                [self showAlertMessage:@"请选择地区"];
            }
            else
            {
                
                
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                NSString* Token = [user objectForKey:@"tokenId"];
                NSMutableDictionary *params = [NSMutableDictionary new];
                [params setValue:Token forKey:@"tokenId"];
                [params setValue:@"U" forKey:@"editType"];
                
                [params setValue:commdity.code forKey:@"addressId"];
                [params setValue:commdity.memberId forKey:@"memberId"];
                [params setValue:isDefault forKey:@"isDefault"];
                [params setValue:self.jzdprovince forKey:@"provinceName"];
                [params setValue:self.jzdcity forKey:@"cityName"];
                [params setValue:self.jzddistrict forKey:@"regionName"];
                [params setValue:UserNameTextField.text forKey:@"name"];
                [params setValue:PasswordTextField.text forKey:@"phone"];
                
                
                [params setValue:PasswordTextField1.text forKey:@"address"];
                NSLog(@"33333333====%@",params);
                [HttpRequest requestData:@"Business/Member/editAddress" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                    if (isSuccess) {
                        
                        NSLog(@"33333333====%@",severDataDict.dataDict);
                        
                        if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                            [Global hideProgressHUDInternal:self.view];
                            
                            
                            
                            [self.navigationController popViewControllerAnimated:YES];
                            
                            
                            
                            
                        }else{
                            
                            
                            
                            NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                            
                            [Global showTextHud:requestResultStr view:self.view];
                            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                                
                                
                                
                                
                                
                                
                                TheLoginViewController* Login=[[TheLoginViewController alloc]init];
                                
                                [self.navigationController pushViewController:Login animated:YES];
                                
                            }
                        }
                    }
                    else
                    {
                        [Global hideProgressHUDInternal:self.view];
                        
                        [Global showTextHudSub:@"连接失败" view:self.view];
                    }
                    
                }];
                
            }}
    }
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}



- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)OnTapDone{
    [UserNameTextField resignFirstResponder];
    [PasswordTextField resignFirstResponder];
    [PasswordTextField1 resignFirstResponder];
    
    
    
}
//回收键盘
-(void)closeKeyBoad
{
    [UserNameTextField resignFirstResponder];
    [PasswordTextField resignFirstResponder];
    [PasswordTextField1 resignFirstResponder];
    
    
}




@end
